package com.tx.admin.characterfetter.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.tx.admin.characterfetter.service.CharacterFetterService;
import com.tx.admin.characterfetter.pojo.po.CharacterFetter;
import com.tx.admin.characterfetter.mapper.CharacterFetterMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.tx.core.common.enums.CommonResponseEnum;
import com.tx.core.util.PageUtils;
import com.tx.core.util.BeanCopyUtils;
import com.tx.core.util.Utils;
import com.tx.core.common.entity.PageResult;
import com.tx.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;
import com.tx.admin.characterfetter.pojo.dto.CharacterFetterCreateDTO;
import com.tx.admin.characterfetter.pojo.dto.CharacterFetterUpdateDTO;
import com.tx.admin.characterfetter.pojo.dto.CharacterFetterListDTO;
import com.tx.admin.characterfetter.pojo.dto.CharacterFetterImportDTO;
import com.tx.core.common.entity.ImportExcelDTO;
import com.tx.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.tx.core.util.FileUtils;
import com.tx.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.tx.admin.characterfetter.pojo.vo.CharacterFetterVO;

/**
 * <p>
 * 人物关系表 服务实现类
 * </p>
 *
 * @author sz-admin
 * @since 2025-08-05
 */
@Service
@RequiredArgsConstructor
public class CharacterFetterServiceImpl extends ServiceImpl<CharacterFetterMapper, CharacterFetter> implements CharacterFetterService {
    @Override
    public void create(CharacterFetterCreateDTO dto){
        CharacterFetter characterFetter = BeanCopyUtils.copy(dto, CharacterFetter.class);
        save(characterFetter);
    }

    @Override
    public void update(CharacterFetterUpdateDTO dto){
        CharacterFetter characterFetter = BeanCopyUtils.copy(dto, CharacterFetter.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(CharacterFetter::getId, dto.getId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        saveOrUpdate(characterFetter);
    }

    @Override
    public PageResult<CharacterFetterVO> page(CharacterFetterListDTO dto){
        Page<CharacterFetterVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto), CharacterFetterVO.class);
        return PageUtils.getPageResult(page);
    }

    @Override
    public List<CharacterFetterVO> list(CharacterFetterListDTO dto){
        return listAs(buildQueryWrapper(dto), CharacterFetterVO.class);
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public CharacterFetterVO detail(Object id){
        CharacterFetter characterFetter = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(characterFetter);
        return BeanCopyUtils.copy(characterFetter, CharacterFetterVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<CharacterFetterImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), CharacterFetterImportDTO.class, true);
        List<CharacterFetterImportDTO> list = excelResult.getList();
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(CharacterFetterListDTO dto, HttpServletResponse response) {
        List<CharacterFetterVO> list = list(dto);
        String fileName = "人物关系表模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "人物关系表", CharacterFetterVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(CharacterFetterListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(CharacterFetter.class);
        if (Utils.isNotNull(dto.getBelongId())) {
            wrapper.eq(CharacterFetter::getBelongId, dto.getBelongId());
        }
        if (Utils.isNotNull(dto.getType())) {
            wrapper.eq(CharacterFetter::getType, dto.getType());
        }
        if (Utils.isNotNull(dto.getExt())) {
            wrapper.eq(CharacterFetter::getExt, dto.getExt());
        }
        if (Utils.isNotNull(dto.getExt1())) {
            wrapper.eq(CharacterFetter::getExt1, dto.getExt1());
        }
        if (Utils.isNotNull(dto.getExt2())) {
            wrapper.eq(CharacterFetter::getExt2, dto.getExt2());
        }
        if (Utils.isNotNull(dto.getExt3())) {
            wrapper.eq(CharacterFetter::getExt3, dto.getExt3());
        }
        if (Utils.isNotNull(dto.getExt4())) {
            wrapper.eq(CharacterFetter::getExt4, dto.getExt4());
        }
        if (Utils.isNotNull(dto.getExt5())) {
            wrapper.eq(CharacterFetter::getExt5, dto.getExt5());
        }
        if (Utils.isNotNull(dto.getExt6())) {
            wrapper.eq(CharacterFetter::getExt6, dto.getExt6());
        }
        if (Utils.isNotNull(dto.getExt7())) {
            wrapper.eq(CharacterFetter::getExt7, dto.getExt7());
        }
        return wrapper;
    }
}